package com.kede.app.service.impl;

import com.kede.app.domain.LeescoreAddress;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.LeescoreAddressRepository;
import com.kede.app.service.LeescoreAddressService;
import com.kede.app.service.dto.LeescoreAddressDto;
import com.kede.app.service.dto.LeescoreAddressQueryCriteria;
import com.kede.app.service.mapstruct.LeescoreAddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-13
**/
@Service
@RequiredArgsConstructor
public class LeescoreAddressServiceImpl implements LeescoreAddressService {

    private final LeescoreAddressRepository leescoreAddressRepository;
    private final LeescoreAddressMapper leescoreAddressMapper;

    @Override
    public Map<String,Object> queryAll(LeescoreAddressQueryCriteria criteria, Pageable pageable){
        Page<LeescoreAddress> page = leescoreAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(leescoreAddressMapper::toDto));
    }

    @Override
    public List<LeescoreAddressDto> queryAll(LeescoreAddressQueryCriteria criteria){
        return leescoreAddressMapper.toDto(leescoreAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LeescoreAddressDto findById(Integer id) {
        LeescoreAddress leescoreAddress = leescoreAddressRepository.findById(id).orElseGet(LeescoreAddress::new);
        ValidationUtil.isNull(leescoreAddress.getId(),"LeescoreAddress","id",id);
        return leescoreAddressMapper.toDto(leescoreAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeescoreAddressDto create(LeescoreAddress resources) {
        return leescoreAddressMapper.toDto(leescoreAddressRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LeescoreAddress resources) {
        LeescoreAddress leescoreAddress = leescoreAddressRepository.findById(resources.getId()).orElseGet(LeescoreAddress::new);
        ValidationUtil.isNull( leescoreAddress.getId(),"LeescoreAddress","id",resources.getId());
        leescoreAddress.copy(resources);
        leescoreAddressRepository.save(leescoreAddress);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            leescoreAddressRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LeescoreAddressDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeescoreAddressDto leescoreAddress : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", leescoreAddress.getUid());
            map.put("邮编", leescoreAddress.getZip());
            map.put(" mobile",  leescoreAddress.getMobile());
            map.put(" country",  leescoreAddress.getCountry());
            map.put("省 / 区", leescoreAddress.getRegion());
            map.put("城市", leescoreAddress.getCity());
            map.put("县 / 区", leescoreAddress.getXian());
            map.put(" address",  leescoreAddress.getAddress());
            map.put("默认收货地址", leescoreAddress.getStatus());
            map.put("创建时间", leescoreAddress.getCreatetime());
            map.put("姓氏", leescoreAddress.getFirstname());
            map.put("名字", leescoreAddress.getLastname());
            map.put("逻辑删除", leescoreAddress.getIsdel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}